package com.pulisher.controller;

import com.pulisher.bean.CourseSubjectOrderBean;
import com.pulisher.bean.PlayChapterBean;
import com.pulisher.service.PlayChapterService;
import com.pulisher.util.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Create 2023/9/11 21:02
 */
@RestController
@RequestMapping("/play")
public class PlayChapterController {

    @Autowired
    PlayChapterService playChapterService;

    //TODO 1、各章节视频播放详情
    @RequestMapping(value = "/chapter",method = RequestMethod.POST)
    public String getCourseOrder(@RequestParam(value = "date",defaultValue = "0") String date){

        if (date.equals("0")){
            date = DateFormatUtil.now().toString();
        }

        List<PlayChapterBean> chapterBeanList = playChapterService.getPlayChapter(date);
        if (chapterBeanList==null||chapterBeanList.size()==0){
            return "";
        }

        StringBuilder columns = new StringBuilder();
        columns.append("{\n\"name\": \"章节\",\n\"id\": \"chapter_name\"\n},")
                .append("{\n\"name\": \"视频播放次数\",\n\"id\": \"video_play_ct\"\n},")
                .append("{\n\"name\": \"观看人数\",\n\"id\": \"user_ct\"\n},")
                .append("{\n\"name\": \"人均观看时长\",\n\"id\": \"per_time\"\n},")
                .append("{\n\"name\": \"累积播放时长\",\n\"id\": \"dur_time\"\n}");
        StringBuilder rows = new StringBuilder();
        for (int i = 0; i < chapterBeanList.size(); i++) {
            PlayChapterBean chapterBean = chapterBeanList.get(i);
            rows.append("{\n\"chapter_name\": \""+chapterBean.getChapter_name()+"\"," +
                    "\n\"video_play_ct\": \""+chapterBean.getVideoPlayCt()+"\"," +
                    "\n\"user_ct\": \""+chapterBean.getUserCt()+"\"," +
                    "\n\"per_time\": \""+chapterBean.getPerTime().setScale(2, BigDecimal.ROUND_HALF_UP)+"\","+
                    "\n\"dur_time\": \""+chapterBean.getDurTime()+"\"}");
            if (i<chapterBeanList.size()-1){
                rows.append(",");
            }
        }

        return "{"+
                "\"status\": 0,"+
                "\"msg\": \"\","+
                "\"data\": {"+
                "\"columns\": ["+columns+
                "],\n"+
                "\"rows\": ["+rows+
                "]"+
                "}"+
                "}";

    }

}
